package jude.java.money.provide;

import java.util.List;

public abstract class LimitedPagingProvider<T> extends PagingProvider<T> {
	
	private static final int PAGE_SIZE = 100;
	private int limit;

	public LimitedPagingProvider(int limit) {
		super(getPageSize(limit), limit > 0);
		if (limit % pageSize != 0)
			limit = (limit / pageSize + 1) * pageSize;
		this.limit = limit;
	}
	
	private static int getPageSize(int limit) {
		if (limit > 0 && limit < PAGE_SIZE + PAGE_SIZE)
			return limit;
		return PAGE_SIZE;
	}
	
	@Override
	protected final int getTotalCount() {
		if (limit > 0)
			return limit;
		return getTotalCount2();
	}


	@Override
	protected final List<T> loadRecords(int pageSize, int pageIndex) {
		if (limit > 0) {
			if (!isReverse()) {
				if (pageSize * pageIndex >= limit)
					return null;
			}
		}
		return loadRecords2(pageSize, pageIndex);
	}
	
	protected int getTotalCount2() {
		throw new RuntimeException("not realized");
	}
	protected abstract List<T> loadRecords2(int pageSize, int pageIndex);
	
	@Override
	public void setRecycle(boolean recycle) {
		throw new RuntimeException("not support");
	}
}
